package com.example.demo.controller;

import com.example.demo.entity.Salespersons;
import com.example.demo.service.SalespersonsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/salespersons")
public class SalespersonsController {

    @Autowired
    private SalespersonsService salespersonsService;

    @GetMapping
    public List<Salespersons> getAllSalespersons() {
        return salespersonsService.list();
    }

    @GetMapping("/{id}")
    public Salespersons getSalespersonById(@PathVariable Integer id) {
        return salespersonsService.getById(id);
    }

    @PostMapping
    public boolean createSalesperson(@RequestBody Salespersons salesperson) {
        return salespersonsService.save(salesperson);
    }

    @PutMapping("/{id}")
    public boolean updateSalesperson(@PathVariable Integer id, @RequestBody Salespersons salesperson) {
        salesperson.setSalespersonId(id); // 假设实体中有setSalespersonId方法
        return salespersonsService.updateById(salesperson);
    }

    @DeleteMapping("/{id}")
    public boolean deleteSalesperson(@PathVariable Integer id) {
        return salespersonsService.removeById(id);
    }
}
